package com.feri.fc.caregiver.service.impl;

import com.feri.fc.caregiver.service.MinioService;
import com.feri.fc.minio.MinioUtil;
import com.feri.fc.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * 千锋Java,为你而生！
 *
 * @Author: Feri
 * @Date:2024/11/26 14:41
 * @Modified:
 */
@Service
public class MinioServiceImpl implements MinioService {
    @Override
    public R<String> upload(MultipartFile file) {
        if(file.isEmpty()){
            return R.fail("亲，请选择上传的文件！");
        }
        String fname= MinioUtil.rename(file.getOriginalFilename());
        try {
            String url = MinioUtil.upload(fname, file.getBytes());
            if(StringUtils.hasLength(url)){
                return R.ok(url);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return R.fail();
    }

    @Override
    public R deleteFile(String objName) {
        if(MinioUtil.deleteObject(objName)){
            return R.ok();
        }
        return R.fail();
    }
}
